# 定义变量
CXX = g++
CXXFLAGS = -std=c++14 -g0 -pipe -Wall -Wextra -pedantic -O3 -DNDEBUG
CPP_HEAD_SUFFIX = .h
CPP_SOURCE_SUFFIX = .cc
PROJECT_NAME = OJLB
INCLUDE_DIR = ./include
SRC_DIR = ./src
DYNAMIC_LIB_DIR = ./3rdparty/dynamic
STATIC_LIB_DIR = ./3rdparty/static
BUILD_DIR = ./build
TESES_DIR = ./tests
TARGET = $(BUILD_DIR)/$(PROJECT_NAME).out
TARGET_TESE = $(TESES_DIR)/$(PROJECT_NAME).out
ALL_SOURCE = $(shell find $(SRC_DIR) -name '*$(CPP_SOURCE_SUFFIX)')
ALL_SOURCE := $(filter-out $(SRC_DIR)/main$(CPP_SOURCE_SUFFIX), $(ALL_SOURCE))
ALL_LIB_FLAG = -lpthread -lgtest -lspdlog -ljsoncpp -lhttplib
MAIN_CC = ./src/main.cc
TEST_CC = ./tests/test_class.cc

# 编译项目
$(TARGET): $(TARGET_TESE) $(MAIN_CC) $(ALL_SOURCE)
	time ccache $(CXX) \
	$(CXXFLAGS) \
	$(MAIN_CC) $(ALL_SOURCE) \
	-I $(INCLUDE_DIR) \
	-L $(DYNAMIC_LIB_DIR) \
	-L $(STATIC_LIB_DIR) \
	$(ALL_LIB_FLAG) \
	-o $@

# 编译测试
$(TARGET_TESE): $(BUILD_DIR) $(TEST_CC) $(ALL_SOURCE)
	time ccache $(CXX) \
	$(CXXFLAGS) \
	$(TEST_CC) $(ALL_SOURCE) \
	-I $(INCLUDE_DIR) \
	-L $(DYNAMIC_LIB_DIR) \
	-L $(STATIC_LIB_DIR) \
	$(ALL_LIB_FLAG) \
	-o $@

# 创建构建
$(BUILD_DIR): print-vars
	mkdir -p $(BUILD_DIR)

# 打印变量
print-vars: clean
	@echo "-- get --"
	@echo "CXX = $(CXX)"
	@echo "CXXFLAGS = $(CXXFLAGS)"
	@echo "CPP_HEAD_SUFFIX = $(CPP_HEAD_SUFFIX)"
	@echo "CPP_SOURCE_SUFFIX = $(CPP_SOURCE_SUFFIX)"
	@echo "PROJECT_NAME = $(PROJECT_NAME)"
	@echo "INCLUDE_DIR = $(INCLUDE_DIR)"
	@echo "SRC_DIR = $(SRC_DIR)"
	@echo "DYNAMIC_LIB_DIR = $(DYNAMIC_LIB_DIR)"
	@echo "STATIC_LIB_DIR = $(STATIC_LIB_DIR)"
	@echo "BUILD_DIR = $(BUILD_DIR)"
	@echo "TESES_DIR = $(TESES_DIR)"
	@echo "TARGET = $(TARGET)"
	@echo "TARGET_TESE = $(TARGET_TESE)"
	@echo "ALL_SOURCE = $(ALL_SOURCE)"
	@echo "ALL_LIB_FLAG = $(ALL_LIB_FLAG)"
	@echo "MAIN_CC = $(MAIN_CC)"
	@echo "TEST_CC = $(TEST_CC)"
	@echo "-- end --"

# 清理构建
.PHONY: clean
clean:
	rm -rf $(BUILD_DIR)
